package com.tianshi.order.mapper;


import com.tianshi.entity.Orders;
import org.apache.ibatis.annotations.*;
import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    /**
     * 根据订单号删除订单
     * @param orderId
     * @return
     */
    @Delete("DELETE FROM orders WHERE order_Id= #{orderId}")
    int deleteByOrderId(Long orderId);

    /**
     * 查询所有订单
     * @return
     */
    @Select("SELECT * FROM orders")
    List<Orders> checkAllOrders();

    /**
     * 根据用户号查询订单
     * @param userId
     * @return
     */
    @Select("SELECT * FROM orders WHERE user_id = #{userId}")
    List<Orders> getByUserId(Long userId);

    @Update("UPDATE orders SET order_status = #{orderStatus} WHERE order_sn = #{orderSn}")
    int updateByOrderSn(Orders orders);



    
    //把订单id赋值给订单
    @Options(useGeneratedKeys = true, keyProperty = "orderId", keyColumn = "order_id")
    @Insert("insert into orders(order_sn, user_id, address_id, total_amount, pay_amount, freight_amount, order_status, payment_method, payment_time, created_at) values(#{orderSn}, #{userId}, #{addressId}, #{totalAmount}, #{payAmount}, #{freightAmount}, #{orderStatus}, #{paymentMethod}, #{paymentTime}, #{createdAt})")
   int insert(Orders orders);

    @Select("select * from orders where user_id = #{userId} and created_at = #{createdAt}")
    Orders selectByUserId(Long userId, LocalDateTime createdAt);


    @Update("UPDATE orders SET total_amount = #{totalAmount}, pay_amount = #{payAmount} WHERE order_id = #{orderId}")
    int updateByOrderId(@Param("orderId") Long orderId,
                        @Param("totalAmount") Double totalAmount,
                        @Param("payAmount") Double payAmount);

    /**
     * 根据订单号查询订单
     * @param orderId
     * @return
     */
    @Select("SELECT * FROM orders WHERE order_id = #{orderId}")
    Orders selectByOrderId(Long orderId);
    @Select("SELECT * FROM orders WHERE user_id = #{userId}")
    List<Orders> checkOrderByUserId(Long userId);
}
